package com.wheel.autoconfigure.mybaits;

import com.wheel.core.utils.Log;
import com.wheel.mybatis.plugins.PropertyNamingStrategyInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource({ "classpath:application-mybatis.properties" })
@EnableConfigurationProperties({ MybatisConfigurationProperties.class })
public class MybatisAutoConfigure {

	private static final Logger log = LoggerFactory.getLogger(MybatisAutoConfigure.class);

	private MybatisConfigurationProperties properties;

	public MybatisAutoConfigure(MybatisConfigurationProperties properties) {
		this.properties = properties;
	}

	@Bean
	@ConditionalOnProperty(name = "w.mybatis.interceptor.propertyNamingStrategy")
	public Interceptor mapResultHumpInterceptor() {
		Log.registered(log);
		return new PropertyNamingStrategyInterceptor(properties.getPropertyNamingStrategy());
	}

}